﻿using System;
using UnityEngine;

[Serializable]
public class BezierControlPoint
{
    [SerializeField]
    private Vector3 m_position;
    [SerializeField]
    private Vector3 m_tangentEnd;
    [SerializeField]
    private Vector3 m_tangentStart;
    public BezierControlPointMode mode = BezierControlPointMode.Auto;

    public BezierControlPoint(Vector3 position)
    {
        this.position = position;
        this.tangentStart = position;
        this.tangentEnd = position;
        this.tangentStart += Vector3.left;
        this.tangentEnd += Vector3.right;
    }

    public Vector3 Evaluate(BezierControlPoint prevPoint, BezierControlPoint nextPoint, float t)
    {
        if (nextPoint == null)
        {
            return this.position;
        }
        if (this.mode == BezierControlPointMode.Straight)
        {
            Vector3 vector = nextPoint.position - this.position;
            Vector3 vector2 = this.position + ((Vector3) (vector / 3f));
            Vector3 vector3 = nextPoint.position - ((Vector3) (vector / 3f));
            this.m_tangentEnd = vector2;
            nextPoint.m_tangentStart = vector3;
        }
        if (this.mode == BezierControlPointMode.Auto)
        {
            if (prevPoint == null)
            {
                return this.position;
            }
            Vector3 vector4 = nextPoint.position - prevPoint.position;
            Vector3 vector5 = nextPoint.position - this.position;
            Vector3 vector6 = prevPoint.position - this.position;
            this.m_tangentStart = this.position - ((Vector3) (vector4.normalized * (vector6.magnitude / 3f)));
            this.m_tangentEnd = this.position + ((Vector3) (vector4.normalized * (vector5.magnitude / 3f)));
        }
        return Bezier.GetPoint(this.position, this.tangentEnd, nextPoint.tangentStart, nextPoint.position, t);
    }

    public Vector3 position
    {
        get
        {
            return this.m_position;
        }
        set
        {
            Vector3 vector = value - this.m_position;
            this.m_tangentStart += vector;
            this.m_tangentEnd += vector;
            this.m_position = value;
        }
    }

    public Vector3 tangentEnd
    {
        get
        {
            return this.m_tangentEnd;
        }
        set
        {
            Vector3 vector = this.m_tangentEnd - this.position;
            if (this.mode == BezierControlPointMode.Mirror)
            {
                this.m_tangentStart = -vector + this.position;
            }
            if (this.mode == BezierControlPointMode.Align)
            {
                Vector3 vector2 = this.m_tangentStart - this.position;
                float magnitude = vector2.magnitude;
                this.m_tangentStart = ((Vector3) (-vector.normalized * magnitude)) + this.position;
            }
            this.m_tangentEnd = value;
        }
    }

    public Vector3 tangentStart
    {
        get
        {
            return this.m_tangentStart;
        }
        set
        {
            Vector3 vector = this.m_tangentStart - this.position;
            if (this.mode == BezierControlPointMode.Mirror)
            {
                this.m_tangentEnd = -vector + this.position;
            }
            if (this.mode == BezierControlPointMode.Align)
            {
                Vector3 vector2 = this.m_tangentEnd - this.position;
                float magnitude = vector2.magnitude;
                this.m_tangentEnd = ((Vector3) (-vector.normalized * magnitude)) + this.position;
            }
            this.m_tangentStart = value;
        }
    }
}

